home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Libraries / grayimage / vrectangle.cc < prev    next >
Encoding:
Text File  |  1994-06-30  |  11.5 KB  |  345 lines  |  [TEXT/R*ch]

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /*
  3.  ************************************************************************
  4.  *
  5.  *               Grayscale Image
  6.  *
  7.  *      Verify the class Rectangle member functions and friends
  8.  *
  9.  * $Id: vrectangle.cc,v 1.1 1992/08/20 14:31:52 oleg Exp $
  10.  *
  11.  ************************************************************************
  12.  */
  13.  
  14. #include "image.h"
  15. #include <builtin.h>
  16. #include <ostream.h>
  17.  
  18. static IMAGE Test_image(16,32,8); //(256,512,8);
  19.  
  20. /*
  21.  *----------------------------------------------------------------------
  22.  *                   Image - Rectangle conversions
  23.  */
  24.  
  25. static void verify_image_rectangle_conversions(void)
  26. {
  27.   cout << "\nVerify Rectangle to image conversions\n";
  28.  
  29.   const int pattern = 145;
  30.  
  31.   {
  32.     cout << "\tVerify the clipped image has now nothing to do with the "
  33.             " parent\n";
  34.     Test_image = pattern;
  35.     IMAGE im1 = Test_image.square_of(min(Test_image.q_nrows(),
  36.                      Test_image.q_ncols()),rowcol(0,0));
  37.     im1 += 1;
  38.     assert( Test_image == pattern );
  39.  
  40.     IMAGE im2 ((Rectangle) Test_image);
  41.     assert( im2 == Test_image );
  42.  
  43.     im2 += 1;
  44.     assert( !(im2 == Test_image) );
  45.     assert( Test_image == pattern );
  46.  
  47.     IMAGE im3 = Test_image.rectangle(rowcol(0,0),
  48.                      rowcol(Test_image.q_nrows()-1,
  49.                         Test_image.q_ncols()-1));
  50.     assert( im3 == Test_image );
  51.     Test_image -= 1;
  52.     assert( !(im2 == Test_image) );
  53.     assert( Test_image == pattern-1 );
  54.     assert( im3 == pattern );
  55.   }
  56.  
  57.   {
  58.     cout << "\tCheck two different clippings from the image\n";
  59.     Test_image = pattern;
  60.     const int size = min(Test_image.q_nrows(),Test_image.q_ncols()) / 2;
  61.     if( size > 3 )
  62.     {
  63.       Test_image.square_of(size,rowcol(1,3)) += 5;
  64.       IMAGE im1(Test_image.rectangle(rowcol(0,2),rowcol(size-1,2+size-1)));
  65.       im1 += 5;
  66.       im1.rectangle(rowcol(1,1),rowcol(1+size-2,1+size-2)) -= 5;
  67.       IMAGE im2(Test_image.square_of(size,rowcol(1,3)));
  68.       assure( im1 == im2, "Image are different" );
  69.     }
  70.   }
  71.  
  72.   {
  73.     cout << "\tCheck clipping 'degenerated' rectangles\n";
  74.  
  75.     IMAGE im1 = Test_image.rectangle(rowcol(0,0),
  76.                      rowcol(0,Test_image.q_ncols()-1));
  77.     assert( im1.q_nrows() == 1 && im1.q_ncols() == Test_image.q_ncols() );
  78.  
  79.     IMAGE im2 = Test_image.rectangle(rowcol(0,1),
  80.                      rowcol(Test_image.q_nrows()-1,1));
  81.     assert( im2.q_ncols() == 1 && im2.q_nrows() == Test_image.q_nrows() );
  82.   }
  83.  
  84.   {
  85.     cout << "\tImage-to-image assignments considered as rectangles\n";
  86.     IMAGE im = Test_image;
  87.     im.clear();
  88.     Test_image = pattern;
  89.     assert( !(Test_image == im) );
  90.     Rectangle(im) = (Rectangle)Test_image;
  91.     assert( Test_image == im );
  92.     assert( im == pattern );
  93.   }
  94.  
  95.   cout << "\nDone\n\n";
  96. }
  97.  
  98.  
  99. /*
  100.  *----------------------------------------------------------------------
  101.  *           Testing operations on square blocks of images
  102.  */
  103.                     // Verify the pixels over the square
  104.                     // area have the value what is expected
  105. static void verify_pixel_value_over_square
  106.     (const int size, const rowcol rc, const GRAY val)
  107. {
  108.   register int i,j;
  109.   for(i=rc.row(); i<rc.row()+size; i++)
  110.     for(j=rc.col(); j<rc.col()+size; j++)
  111.       if( Test_image(i,j) != val )
  112.     _error("Pixel [%d,%d] has the value 0x%x different from expected 0x%x",
  113.            i,j,Test_image(i,j),val);
  114.  
  115.                     // Test the values beyond the 
  116.                     // square area boundaries.
  117.                     // They are supposed to be different
  118.   if( 
  119.      ( rc.row() + size < Test_image.q_nrows() &&
  120.        (i = rc.row()+size, j = rc.col()+size-1, Test_image(i,j) == val) ) ||
  121.      ( rc.col() + size < Test_image.q_ncols() &&
  122.        (i = rc.row()+size-1, j = rc.col()+size, Test_image(i,j) == val) ) ||
  123.      ( rc.row() > 0 &&
  124.        (i = rc.row()-1, j = rc.col(), Test_image(i,j) == val) ) ||
  125.      ( rc.col() > 0 &&
  126.        (i = rc.row(), j = rc.col()-1, Test_image(i,j) == val) )
  127.    )
  128.     _error("Pixel [%d,%d] beyond the square area has the same value 0x%x "
  129.        " as pixels within the area\n"
  130.        " The prev operation must have written beyond the square area",
  131.        i,j,val);
  132. }
  133.  
  134. static void test_pixel_op_over_square(const int size)
  135. {
  136.   const int pattern = 154;
  137.   register int i,j;
  138.  
  139.   cout << "\n\nTest reading/writing pixels and pixel modifications\n"
  140.           "over square blocks\n\n";
  141.   cout << "Size of the square block being tested " << size << "\n\n";
  142.  
  143.   Test_image.clear();
  144.  
  145.   cout << "\nBlock writing a pattern 0x" << hex(pattern) << "...\n";
  146.   cout << "and performing the entire set of tests "
  147.           "as for the whole image above ...";
  148.  
  149.   for(i=0; i<Test_image.q_nrows() - size; i+=5)
  150.     for(j=0; j<Test_image.q_ncols() - size; j+=3)
  151.     {
  152.       Test_image.sure_within(rowcol(i,j));
  153.       Test_image.square_of(size,rowcol(i,j)) = pattern;
  154.       verify_pixel_value_over_square(size,rowcol(i,j),pattern);
  155.  
  156.       double sum;
  157.       if( ( sum = sum_over(Test_image.square_of(size,rowcol(i,j))) ) !=
  158.       size*size*(double)pattern )
  159.     _error("Total sum of all the pixels over the square %g differs "
  160.            "from the expected value %g",sum,size*size*(double)pattern);
  161.  
  162.       Test_image.square_of(size,rowcol(i,j)) ^= ((1<<GRAY_MAXBIT)-1);
  163.       verify_pixel_value_over_square(size,rowcol(i,j),0xffff - pattern);
  164.  
  165.       const int test_val = 0x7e;
  166.       Test_image.square_of(size,rowcol(i,j)) = test_val;
  167.       verify_pixel_value_over_square(size,rowcol(i,j),test_val);
  168.  
  169.       Test_image.square_of(size,rowcol(i,j)) += 7;
  170.       verify_pixel_value_over_square(size,rowcol(i,j),test_val+7);
  171.       assure( Test_image >= 0, "Negative pixel unexpected");
  172.  
  173.       Test_image.square_of(size,rowcol(i,j)) -= 2*test_val;
  174.       verify_pixel_value_over_square(size,rowcol(i,j),test_val+7-2*test_val);
  175.       assure( !(Test_image > 0), "Non-Negative pixel unexpected");
  176.  
  177.       Test_image.square_of(size,rowcol(i,j)) -= -2*test_val+7;
  178.       verify_pixel_value_over_square(size,rowcol(i,j),test_val);
  179.  
  180.       Test_image.square_of(size,rowcol(i,j)) |= 0x03;
  181.       verify_pixel_value_over_square(size,rowcol(i,j),test_val | 0x03);
  182.  
  183.       Test_image.square_of(size,rowcol(i,j)) &= 0xff - 0x03;
  184.       verify_pixel_value_over_square(size,rowcol(i,j),test_val & (0xff-0x03));
  185.  
  186.       Test_image.square_of(size,rowcol(i,j)) ^= 0x02;
  187.       verify_pixel_value_over_square(size,rowcol(i,j),test_val);
  188.  
  189.       Test_image.square_of(size,rowcol(i,j)) *= 2;
  190.       verify_pixel_value_over_square(size,rowcol(i,j),2*test_val);
  191.  
  192.       Test_image.square_of(size,rowcol(i,j)) <<= 1;
  193.       verify_pixel_value_over_square(size,rowcol(i,j),4*test_val);
  194.  
  195.       Test_image.square_of(size,rowcol(i,j)) >>= 2;
  196.       verify_pixel_value_over_square(size,rowcol(i,j),test_val);
  197.  
  198.       Test_image.square_of(size,rowcol(i,j)) = 0;
  199.     }
  200.  
  201.   cout << "\nDone\n\n";
  202. }
  203.  
  204.  
  205. /*
  206.  *----------------------------------------------------------------------
  207.  *           Testing operations on rectangular blocks of images
  208.  */
  209.                     // Verify the pixels in the rectangle
  210.                     // have the value what is expected
  211. static void verify_pixel_value_over_rectangle
  212.     (const rowcol uppleft, const rowcol lowright, const GRAY val)
  213. {
  214.   register int i,j;
  215.   for(i=uppleft.row(); i<=lowright.row(); i++)
  216.     for(j=uppleft.col(); j<=lowright.col(); j++)
  217.       if( Test_image(i,j) != val )
  218.     _error("Pixel [%d,%d] has the value 0x%x different from expected 0x%x",
  219.            i,j,Test_image(i,j),val);
  220.  
  221.                     // Test the values beyond the 
  222.                     // rectangular area boundaries.
  223.                     // They ought to be different
  224.   rowcol rc;
  225.   if( 
  226.      ( lowright.row()+1 < Test_image.q_nrows() &&
  227.        Test_image(rc = lowright + rowcol(1,0)) == val ) ||
  228.      ( lowright.col()+1 < Test_image.q_ncols() &&
  229.        Test_image(rc = lowright + rowcol(0,1)) == val ) ||
  230.      ( uppleft.row() > 0 &&
  231.        Test_image(rc = uppleft - rowcol(1,0)) == val )  ||
  232.      ( uppleft.col() > 0 &&
  233.        Test_image(rc = uppleft - rowcol(0,1)) == val )
  234.    )
  235.     _error("Pixel [%d,%d] beyond the rectangular area has the same value 0x%x "
  236.        " as pixels within the area\n"
  237.        " The prev operation must have written beyond the square area",
  238.        rc.row(),rc.col(),val);
  239. }
  240.  
  241. static void test_pixel_op_over_rectangle(const int nrows, const int ncols)
  242. {
  243.   const int pattern = 104;
  244.   register int i,j;
  245.  
  246.   cout << "\n\nTest reading/writing pixels and pixel modifications\n"
  247.           "over rectangular blocks " << nrows << " by " << ncols << "\n\n";
  248.  
  249.   Test_image.clear();
  250.  
  251.   cout << "\nBlock writing a pattern 0x" << hex(pattern) << "...\n";
  252.   cout << "and performing the entire set of tests "
  253.           "as for the whole image in vimage ...";
  254.  
  255.   for(i=0; i<Test_image.q_nrows() - nrows; i+=5)
  256.     for(j=0; j<Test_image.q_ncols() - ncols; j+=3)
  257.     {
  258.       rowcol uppleft(i,j), lowright(i+nrows-1,j+ncols-1);
  259.       Test_image.sure_within(uppleft);
  260.       Test_image.sure_within(lowright);
  261.       Test_image.rectangle(uppleft,lowright) = pattern;
  262.       verify_pixel_value_over_rectangle(uppleft,lowright,pattern);
  263.  
  264.       double sum;
  265.       if( ( sum = sum_over(Test_image.rectangle(uppleft,lowright)) ) !=
  266.       nrows*ncols*(double)pattern )
  267.     _error("Total sum of all the pixels over the rectangle %g differs "
  268.            "from the expected value %g",sum,nrows*ncols*(double)pattern);
  269.  
  270.       Test_image.rectangle(uppleft,lowright) ^= ((1<<GRAY_MAXBIT)-1);
  271.       verify_pixel_value_over_rectangle(uppleft,lowright,0xffff - pattern);
  272.  
  273.       const int test_val = 0x7e;
  274.       Test_image.rectangle(uppleft,lowright) = test_val;
  275.       verify_pixel_value_over_rectangle(uppleft,lowright,test_val);
  276.  
  277.       Test_image.rectangle(uppleft,lowright) += 7;
  278.       verify_pixel_value_over_rectangle(uppleft,lowright,test_val+7);
  279.       assure( Test_image >= 0, "Negative pixel unexpected");
  280.  
  281.       Test_image.rectangle(uppleft,lowright) -= 2*test_val;
  282.       verify_pixel_value_over_rectangle(uppleft,lowright,
  283.                     test_val+7-2*test_val);
  284.       assure( !(Test_image > 0), "Non-Negative pixel unexpected");
  285.  
  286.       Test_image.rectangle(uppleft,lowright) -= -2*test_val+7;
  287.       verify_pixel_value_over_rectangle(uppleft,lowright,test_val);
  288.  
  289.       Test_image.rectangle(uppleft,lowright) |= 0x03;
  290.       verify_pixel_value_over_rectangle(uppleft,lowright,test_val | 0x03);
  291.  
  292.       Test_image.rectangle(uppleft,lowright) &= 0xff - 0x03;
  293.       verify_pixel_value_over_rectangle(uppleft,lowright,
  294.                     test_val & (0xff-0x03));
  295.  
  296.       Test_image.rectangle(uppleft,lowright) ^= 0x02;
  297.       verify_pixel_value_over_rectangle(uppleft,lowright,test_val);
  298.  
  299.       Test_image.rectangle(uppleft,lowright) *= 2;
  300.       verify_pixel_value_over_rectangle(uppleft,lowright,2*test_val);
  301.  
  302.       Test_image.rectangle(uppleft,lowright) <<= 1;
  303.       verify_pixel_value_over_rectangle(uppleft,lowright,4*test_val);
  304.  
  305.       Test_image.rectangle(uppleft,lowright) >>= 2;
  306.       verify_pixel_value_over_rectangle(uppleft,lowright,test_val);
  307.  
  308.       {
  309.     IMAGE im(Test_image);
  310.     im.rectangle(uppleft,lowright) = test_val;
  311.     Test_image.rectangle(uppleft,lowright) = 0;
  312.     Test_image.rectangle(uppleft,lowright) = 
  313.       im.rectangle(uppleft,lowright);
  314.     verify_pixel_value_over_rectangle(uppleft,lowright,test_val);
  315.       }    
  316.     
  317.       Test_image.rectangle(uppleft,lowright) = 0;
  318.     }
  319.  
  320.   cout << "\nDone\n\n";
  321. }
  322.  
  323. /*
  324.  *------------------------------------------------------------------------
  325.  *            The testing routing module
  326.  */
  327.  
  328. main()
  329. {
  330.   verify_image_rectangle_conversions();
  331.  
  332.   test_pixel_op_over_square(Test_image.q_nrows());
  333.   test_pixel_op_over_square(Test_image.q_nrows()/3);
  334.   test_pixel_op_over_square(1);
  335.  
  336.   test_pixel_op_over_rectangle(Test_image.q_nrows(),Test_image.q_ncols());
  337.   int size = min(Test_image.q_nrows(),Test_image.q_ncols());
  338.   size = ( size > 2 ? size / 2 : size );
  339.   test_pixel_op_over_rectangle(size,size);
  340.   test_pixel_op_over_rectangle((size == 4 ? 5 : 4),size);
  341.   test_pixel_op_over_rectangle(1,size);
  342.   test_pixel_op_over_rectangle(size,1);
  343. }
  344.  
  345.